
    // All AJAX goes in this file. I love jQuery AJAX - so easy.

    $(document).ready(function() {

        // Control favourites
        $('.toggle_favourite').click(function() {
            var link = $(this);
            $.ajax({
                url: '/ajax.php?action=toggle_favourite&cheat_sheet_id=' + link.attr('id'),
                success: function(response){
                    if (response == 'on') {
                        link.attr('src', '/images/icons/heart_delete.png');
                        $('#ln_' + link.attr('id')).html('Remove from Favourites');
                    } else {
                        link.attr('src', '/images/icons/heart_add.png');
                        $('#ln_' + link.attr('id')).html('Add to Favourites');
                    }
                }
            });
        });

        $('.toggle_favourite_link').click(function() {
            var favourite_link = $(this).attr('id');
            $.ajax({
                url: '/ajax.php?action=toggle_favourite&cheat_sheet_id=' + favourite_link,
                success: function(response){
                    if (response == 'on') {
                        $('#' + favourite_link).parent().removeClass('icon_heart_add');
                        $('#' + favourite_link).parent().addClass('icon_heart_delete');
                        $('#' + favourite_link).html('Remove Favourite');
                        alert('Added to Favourites.');
                    } else {
                        $('#' + favourite_link).parent().removeClass('icon_heart_delete');
                        $('#' + favourite_link).parent().addClass('icon_heart_add');
                        $('#' + favourite_link).html('Add Favourite');
                        alert('Removed from Favourites.');
                    }
                }
            });
            return false;
        });

        // Control favourites
        $('.follow_user').click(function() {
            var user_id = $(this).attr('id').replace('user', '');
            var link = $(this);
            $.ajax({
                url: '/ajax.php?action=follow_user&user_id=' + link.attr('id'),
                success: function(response){
                    if (response == 'on') {
                        $('.follow_text_user' + user_id).html('Unfollow');
                        $('.follow_img_user' + user_id).attr('src', '/images/icons/user_delete.png');
                    } else {
                        $('.follow_text_user' + user_id).html('Follow');
                        $('.follow_img_user' + user_id).attr('src', '/images/icons/user_add.png');
                    }
                }
            });
            return false;
        });

    });


    var hoveron = false;
    function setupHoverboxes() {
        hiConfig = {
            sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)
            interval: 100, // number = milliseconds for onMouseOver polling interval
            timeout: 200, // number = milliseconds delay before onMouseOut
            over: function() {
                hoveron = true;
                var item = $(this).children(':first-child'); // Get user_hover item
                var user_name = item.text();
                if (user_name == '') {
                    user_name = item.attr('title');
                }
                item.parent().append('<div class="hoverbox hoverboxloading"><img src="/images/loading.gif" width="16" height="16"><strong style="display:none;">' + user_name + '</strong></div>');
                $.ajax({
                    url: '/ajax.php?action=user_hover&user_name=' + user_name,
                    success: function(data) {
                        $('.hoverbox').remove();
                        if ((data != '') && (hoveron)) {
                            item.parent().append('<div id="hoverbox' + user_name + '" class="hoverbox">' + data + '</div>');
                            // Check right hand side and bring into view if too far over
                        }
                    }
                });
            }, // function = onMouseOver callback (REQUIRED)
            out: function() {
                hoveron = false;
                $('.hoverbox').remove();
                // Next line fixes bug where hoverbox above a table isn't removed (or redrawn) properly
                $('#footer').append('<div style="position: absolute; left: -1000px; box-shadow: #333333 0 0 5px;"></div>');
            } // function = onMouseOut callback (REQUIRED)
        }
        $('.user_hover').wrap('<span class="hover_wrap" />');
        $('.hover_wrap').hoverIntent(hiConfig);
    }

    $(document).ready(function(){
        setupHoverboxes();
    });
